﻿using BP.DA;
using BP.WF.Template;
using BP.WF.Template.Frm;
using BP.Difference;
using System.Web;
using NPOI.SS.Formula.Functions;
using System;
using System.IO;
using BP.Template.Printer;

namespace BP.WF.HttpHandler
{
    /// <summary>
    /// 页面功能实体
    /// </summary>
    public class WF_Admin_FoolFormDesigner_PrintTemplate : BP.WF.HttpHandler.DirectoryPageBase
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public WF_Admin_FoolFormDesigner_PrintTemplate()
        {
        }

        #region  单据模版维护
        /// <summary>
        /// @李国文.
        /// </summary>
        /// <returns></returns>
        public string Bill_Save()
        {
            FrmPrintTemplate bt = new FrmPrintTemplate();

            if (HttpContextHelper.RequestFilesCount == 0)
                return "err@请上传模版.";
            //上传附件
            string filepath = "";
            //IFormFile file = HttpContextHelper.RequestFiles(0);
            string fileName = HttpContextHelper.GetNameByIdx(0);
            fileName = fileName.Substring(fileName.IndexOf(this.GetRequestVal("TB_Name")));
            fileName = fileName.ToLower();

            filepath =  BP.Difference.SystemConfig.PathOfDataUser + "CyclostyleFile/" + fileName;
            //file.SaveAs(filepath);
            HttpContextHelper.UploadFile(HttpContextHelper.RequestFiles(0), filepath);

            bt.NodeID = this.NodeID;
            bt.FrmID = this.FrmID;
            bt.MyPK= this.GetRequestVal("TB_No");

            if (DataType.IsNullOrEmpty(bt.MyPK))
                bt.MyPK = DBAccess.GenerOID("Template").ToString();

            bt.Name = this.GetRequestVal("TB_Name");
            bt.TempFilePath = fileName; //文件.

            //打印的文件类型.
            bt.HisPrintFileType = (PrintFileType)this.GetRequestValInt("DDL_BillFileType");

            //打开模式.
            bt.PrintOpenModel = (PrintOpenModel)this.GetRequestValInt("DDL_BillOpenModel");

            //二维码模式.
            bt.QRModel = (QRModel)this.GetRequestValInt("DDL_QRModel");

            bt.TemplateFileModel = (TemplateFileModel)this.GetRequestValInt("TemplateFileModel");
           

            bt.Save();

            bt.SaveFileToDB("DBFile", filepath); //把文件保存到数据库里. 

            Cache.ClearCache(fileName);
            Cache.ClearCache(fileName+ "Para");

            return "保存成功.";
        }
        public  string PrintTemplate_Save()
        {
            FrmPrintTemplate bt = new FrmPrintTemplate();
            if (HttpContextHelper.RequestFilesCount == 0)
                return "err@请上传模版.";
            //上传附件
            string filepath = "";
            string fileName = HttpContextHelper.GetNameByIdx(0);
            filepath = BP.Difference.SystemConfig.PathOfTemp + fileName;
            HttpContextHelper.UploadFile(HttpContextHelper.RequestFiles(0), filepath);
            
            bt.setMyPK(this.MyPK);
            bt.RetrieveFromDBSources();
            bt.SetPara("FileName", fileName.Substring(0,fileName.LastIndexOf(".")));
            bt.Update();
            bt.SaveFileToDB("DBFile", filepath); //把文件保存到数据库里.

            File.Delete(filepath);
		    Cache.ClearCache(fileName);
		    Cache.ClearCache(fileName+ "Para");
		    //DBAccess.RunSQL("UPDATE Sys_FrmPrintDB SET Ver=(Select Max(Ver)+1 From Sys_FrmPrintDB Where FrmPrintTemplateID='"+bt.getMyPK()+"' AND FrmID = '"+bt.getFrmID()+"') Where FrmPrintTemplateID='"+bt.getMyPK()+"' AND FrmID = '"+bt.getFrmID()+"'");
		    return "保存成功.";
	    }
        /**
	     下载文件.
	    */
        public string PrintTemplate_Download() 
        {
            FrmPrintTemplate en = new FrmPrintTemplate(this.MyPK);
            string subFix = ".rtf";
            string fileModel = en.GetValStringByKey("FileModel");
		    if(fileModel.Equals("VSTOExcel")) subFix=".xls";
		    if(fileModel.Equals("VSTOWord")) subFix=".doc";
		    string filepath = SystemConfig.PathOfTemp + en.Name + subFix;
            DBAccess.GetFileFromDB(filepath,"Sys_FrmPrintTemplate","MyPK",this.MyPK,"DBFile");
		    return "/DataUser/Temp/"+ en.Name+subFix;
        }
        #endregion

    }
}
